﻿$(function () {
    window.progressDialog = new ProgressDialog();
    window.infoDialog = new InfoDialog();
    window.ajaxClass = new AjaxClass();
    window.resizeFlexiGrid = null;

    $("#ButtonBack").button({ icons: { primary: "ui-icon-arrowreturnthick-1-w"} }).click(function () { window.location.href = JsonOriginal.UrlReferrer; });
    $("#ButtonCancel").button({ icons: { primary: "ui-icon-unlocked"} }).click(function () { SetBind(false, "$('#ListTD').parent().css('display', 'none'); $('#PageTR').css('display', 'none'); $('#TabRadio').css('display', 'none');  $('#SearchTR form').css('display', 'none'); $('#List1_SearchButton').css('display', 'none'); $('#ButtonBind').css('display', 'inline'); $('#ButtonCancel').css('display', 'none');"); });
    $("#ButtonBind").button({ icons: { primary: "ui-icon-locked"} }).click(function () { SetBind(true, "$(\"label[for='radio2']\").click(); $('#radio2').click(); $('#ListTD').parent().css('display', 'block'); $('#PageTR').css('display', 'block'); $('#TabRadio').css('display', 'inline'); $('#SearchTR form').css('display', 'inline'); $('#List1_SearchButton').css('display', 'inline'); $('#ButtonCancel').css('display', 'inline'); $('#ButtonBind').css('display', 'none');"); });

    $("#TabRadio").buttonset();
    $("input[name='TabRadio']").change(function () {
        LoadGridData($(this).val());
        this.blur();
    });

    if ($("#TabRadio").css("display") == "inline") {
        $("label[for='radio1']").click();
        $("#radio1").click();
    }

    $("#UpdateDialog").dialog({
        autoOpen: false,
        resizable: false,
        height: 236,
        width: 541,
        modal: true,
        zIndex: 1000,
        buttons: {
            保存: function () {
                if ($("#IsView").is(":checked") || $("#IsEdit").is(":checked") || $("#IsReport").is(":checked")) {
                    var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": "修改绑定", "Parameters": { "@AutoID": $("#AutoID").val(), "@IsView": ($("#IsView").is(":checked") ? 1 : 0), "@IsEdit": ($("#IsEdit").is(":checked") ? 1 : 0), "@IsReport": ($("#IsReport").is(":checked") ? 1 : 0)}}], [{ "OperationKey": window.flexiGrid.listName, "Parameters": { "@SearchKey": window.flexiGrid.searchKey, "@SortName": window.flexiGrid.sortName, "@SortOrder": window.flexiGrid.sortOrder }, "JsonParameter": "{\"记录控制\":[{\"开始记录\":" + ((window.flexiGrid.PageNum - 1) * window.flexiGrid.PageSize) + ",\"返回数量\":" + window.flexiGrid.PageSize + "}]}"}]]) };
                    window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.flexiGrid.Refresh(data.ReturnData[1], window.flexiGrid.sortName, window.flexiGrid.sortOrder, window.flexiGrid.searchKey); $('#UpdateDialog').dialog('close');", "", false, true);
                } else {
                    var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": "删除绑定", "Parameters": { "@AutoID": $("#AutoID").val()}}], [{ "OperationKey": window.flexiGrid.listName, "Parameters": { "@SearchKey": window.flexiGrid.searchKey, "@SortName": window.flexiGrid.sortName, "@SortOrder": window.flexiGrid.sortOrder }, "JsonParameter": "{\"记录控制\":[{\"开始记录\":" + ((window.flexiGrid.PageNum - 1) * window.flexiGrid.PageSize) + ",\"返回数量\":" + window.flexiGrid.PageSize + "}]}"}]]) };
                    window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.flexiGrid.Refresh(data.ReturnData[1], window.flexiGrid.sortName, window.flexiGrid.sortOrder, window.flexiGrid.searchKey); $('#UpdateDialog').dialog('close');", "", false, true);
                }
            },
            取消: function () {
                $(this).dialog("close");
            }
        }
    });
});

function ShowUpdateDialog(AutoID) {
    var DataList = JsonOriginal.Operations[window.flexiGrid.listName].OperationData.rows;
    for (var i = 0; i < DataList.length; i++) {
        if (AutoID == DataList[i].cell[0]) {
            $("#AutoID").val(DataList[i].cell[0]);
            $("#voucher_id").val($("<div>" + DataList[i].cell[1] + "</div>").text());
            $("#voucher_id").attr("title", $("<div>" + DataList[i].cell[1] + "</div>").text());
            $("#ct_user_no").val($("<div>" + DataList[i].cell[2] + "</div>").text());
            $("#ct_user_no").attr("title", $("<div>" + DataList[i].cell[2] + "</div>").text());
            $("#ct_store_no").val($("<div>" + DataList[i].cell[3] + "</div>").text());
            $("#ct_store_no").attr("title", $("<div>" + DataList[i].cell[3] + "</div>").text());
            $("#ct_abbr").val($("<div>" + DataList[i].cell[4] + "</div>").text());
            $("#ct_abbr").attr("title", $("<div>" + DataList[i].cell[4] + "</div>").text());
            if ($("<div>" + DataList[i].cell[5] + "</div>").text() == "有权限") {
                $("#IsView").attr("checked", "checked");
            } else {
                $("#IsView").removeAttr("checked");
            }
            if ($("<div>" + DataList[i].cell[6] + "</div>").text() == "有权限") {
                $("#IsEdit").attr("checked", "checked");
            } else {
                $("#IsEdit").removeAttr("checked");
            }
            if ($("<div>" + DataList[i].cell[7] + "</div>").text() == "有权限") {
                $("#IsReport").attr("checked", "checked");
            } else {
                $("#IsReport").removeAttr("checked");
            }
        }
    }
    $("#UpdateDialog").dialog("open");
}

function LoadGridData(ListName) {
    var SortName = "AutoID";
    if (ListName == "所有客户列表") {
        SortName = "voucher_id";
    }
    if (window.resizeFlexiGrid == null) {
        window.resizeFlexiGrid = new ResizeFlexiGrid(".flexigrid", "#List1", ".flexigrid .bDiv", ["#TitleTR", "#SearchTR", "#PageTR", ".flexigrid .hDiv"], 4, 4);
        window.flexiGrid = new FlexiGrid("flexiGrid", "List1", "ListTD", "PageTR", "SearchTR", "resizeFlexiGrid", [
            { display: '自动编号', name: 'AutoID', width: 50, sortable: true, align: 'left', hide: true },
            { display: '系统编号', name: 'voucher_id', width: 60, sortable: true, align: 'left', hide: false },
            { display: '客户编号', name: 'ct_user_no', width: 80, sortable: true, align: 'left', hide: false },
            { display: '分店编号', name: 'ct_store_no', width: 80, sortable: true, align: 'left', hide: false },
            { display: '简称', name: 'ct_abbr', width: 180, sortable: true, align: 'left', hide: false },
            { display: '浏览权限', name: 'IsView', width: 80, sortable: true, align: 'left', hide: false },
            { display: '修改权限', name: 'IsEdit', width: 80, sortable: false, align: 'left', hide: false },
            { display: '报表权限', name: 'IsReport', width: 80, sortable: false, align: 'left', hide: false },
            { display: '操作', name: 'Operation', width: 160, sortable: false, align: 'left', hide: false }
        ], null, null, "desc", "", false, null, SetOperation);
        window.resizeFlexiGrid.Resize("resizeFlexiGrid");
    }
    window.flexiGrid.sortName = SortName;
    window.flexiGrid.listName = ListName;
    window.flexiGrid.SetListGrid(SortName, "desc");
    window.flexiGrid.LoadData("", "", "", null, null);
}

function SetBind(Bind, SuccessCode) {
    var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": "设置客户绑定", "Parameters": { "@BindCustomer": Bind}}]]) };
    window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", SuccessCode, "", false, true);
}

function SetOperation() {
    if (window.flexiGrid.listName == "所有客户列表") {
        $("#List1 tr").each(function (index, element) {
            if ($(element).find("div:first").html() != "&nbsp;") {
                $(element).find("div:last a:first").remove();
            } else {
                $(element).find("div:last a:eq(1), div:last a:eq(2)").remove();
            }
        });
    }
}

function AddBind(VoucherID) {
    var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": "添加绑定", "Parameters": { "@CustomerVoucher": VoucherID}}], [{ "OperationKey": window.flexiGrid.listName, "Parameters": { "@SearchKey": window.flexiGrid.searchKey, "@SortName": window.flexiGrid.sortName, "@SortOrder": window.flexiGrid.sortOrder }, "JsonParameter": "{\"记录控制\":[{\"开始记录\":" + ((window.flexiGrid.PageNum - 1) * window.flexiGrid.PageSize) + ",\"返回数量\":" + window.flexiGrid.PageSize + "}]}"}]]) };
    window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.flexiGrid.Refresh(data.ReturnData[1], window.flexiGrid.sortName, window.flexiGrid.sortOrder, window.flexiGrid.searchKey);", "", false, true);            
}

function CancelBind(AutoID) {
    var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": "删除绑定", "Parameters": { "@AutoID": AutoID}}], [{ "OperationKey": window.flexiGrid.listName, "Parameters": { "@SearchKey": window.flexiGrid.searchKey, "@SortName": window.flexiGrid.sortName, "@SortOrder": window.flexiGrid.sortOrder }, "JsonParameter": "{\"记录控制\":[{\"开始记录\":" + ((window.flexiGrid.PageNum - 1) * window.flexiGrid.PageSize) + ",\"返回数量\":" + window.flexiGrid.PageSize + "}]}"}]]) };
    window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.flexiGrid.Refresh(data.ReturnData[1], window.flexiGrid.sortName, window.flexiGrid.sortOrder, window.flexiGrid.searchKey);", "", false, true);
}